package Dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DataUtil;

import model.chiTietHDModel;
import model.nhanVienModel;


public class chiTietHDDao {
	public static ArrayList<chiTietHDModel> getAllchiTietHD(){
		ArrayList<chiTietHDModel> listchiTietHD = new ArrayList<chiTietHDModel>();
		try {
			String sql = "SELECT ID,Ten_San_Pham,Ten_Khach_Hang,Ma_San_Pham,So_luong,Ghi_chu,M_hoa_don FROM ChiTietHoaDon";
			ResultSet rs = DataUtil.executeQuery(sql);
			while (rs.next()){
				chiTietHDModel model = new chiTietHDModel();
				model.setId(rs.getInt("ID"));
				model.setTensp(rs.getString("Ten_San_Pham"));
				model.setTenkh(rs.getString("Ten_Khach_Hang"));
				model.setMasp(rs.getInt("Ma_San_Pham"));
				model.setSoluong(rs.getInt("So_luong"));
				model.setGhichu(rs.getString("Ghi_chu"));
				model.setMahd(rs.getInt("M_hoa_don"));
				
				listchiTietHD.add(model);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listchiTietHD;
	}
public static ArrayList<chiTietHDModel> searchchiTietHD(String ten){
		ArrayList<chiTietHDModel> listchiTietHD = new ArrayList<chiTietHDModel>();
		
		try {
			String sql = "SELECT ID,Ten_San_Pham,Ten_Khach_Hang,Ma_San_Pham,So_luong,Ghi_chu,M_hoa_don FROM ChiTietHoaDon WHERE Ten_San_Pham LIKE '%" +ten+ "%'";
			ResultSet rs = DataUtil.executeQuery(sql);
			System.out.println("Result Set:"+rs.getRow());
			while (rs.next()){
				chiTietHDModel model = new chiTietHDModel();
				model.setId(rs.getInt("ID"));
				model.setTensp(rs.getString("Ten_San_Pham"));
				model.setTenkh(rs.getString("Ten_Khach_Hang"));
				model.setMasp(rs.getInt("Ma_San_Pham"));
				model.setSoluong(rs.getInt("So_luong"));
				model.setGhichu(rs.getString("Ghi_chu"));
				model.setMahd(rs.getInt("M_hoa_don"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listchiTietHD;
	}	
public static Boolean deletechiTietHD(chiTietHDModel model){
	Boolean kq = false;
	try {
		String sql = "delete from ChiTietHoaDon where ID = ?";
		PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
		ps.setInt(1, model.getId());
		ps.executeUpdate();
		kq = true;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return kq;
}
public static Boolean updatechiTietHD( chiTietHDModel model){
	Boolean kq = false;
	try {
		String sql = "update ChiTietHoaDon set Ten_San_Pham=?,Ten_Khach_Hang=?, Ma_San_Pham=?,Ghi_Chu=?, So_luong=?,M_hoa_don =? where ID=?";
		PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
		ps.setString(1, model.getTenkh());
		ps.setString(2, model.getTensp());
		ps.setString(3, model.getGhichu());
		ps.setInt(4, model.getMahd());
		ps.setInt(5, model.getSoluong());
		ps.setInt(6, model.getMasp());
		
		ps.executeUpdate();
		kq = true;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return kq;
}
public static Boolean insertchiTietHD( chiTietHDModel model){
	Boolean kq = false;
	
		
	return kq;
}

}
